//
//  LSCashAlipayVC.m
//  HappyStore
//
//  Created by 陈锋 on 16/8/16.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "LSCashAlipayVC.h"
#import "LSCashAlipayCell.h"
#import "LSAddAlipayUserID.h"
#import "LSPayResultVC.h"

static NSString *lSCashAlipayCell = @"LSCashAlipayCell";
@interface LSCashAlipayVC (){

    NSMutableArray *dataArr;
    NSString *fileName;
}

@end

@implementation LSCashAlipayVC

-(void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:YES];
    
    fileName=[DOCUMENTPATHS stringByAppendingPathComponent:@"alipayUserIDFile.plist"];
    
    
    dataArr=[[NSMutableArray arrayWithContentsOfFile:fileName] copy];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"提现";
    self.tableView.backgroundColor = RGBA(247.0, 247.0, 247.0, 1);
    // 注册
    UINib * nib = [UINib nibWithNibName:@"LSCashAlipayCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:lSCashAlipayCell];
    
    //设置返回按钮
    UIBarButtonItem *leftItem = [UIBarButtonItem itemWithTarget:self action:@selector(leftBarButtonItemClick) image:@"Back" highImage:@"Back"];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    //尾视图
    UIView * footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screenW, 50)];
    footView.backgroundColor = RGBA(247.0, 247.0, 247.0, 1);
    
    UIButton * addAplipayBtn = [[UIButton alloc]init];
    addAplipayBtn.x = 40;
    addAplipayBtn.y = 5;
    addAplipayBtn.width = screenW - 80;
    addAplipayBtn.height = 40;
    [addAplipayBtn setTitle:@"添加提现账户" forState:0];
    [addAplipayBtn setTitleColor:RGB(0, 193, 226) forState:0];
    addAplipayBtn.titleLabel.font = [UIFont systemFontOfSize:13.0f];
    addAplipayBtn.backgroundColor = [UIColor whiteColor];
    addAplipayBtn.layer.masksToBounds = YES;
    addAplipayBtn.layer.cornerRadius = 5.0f;
    [addAplipayBtn addTarget:self action:@selector(addNewAlipayUser) forControlEvents:UIControlEventTouchUpInside];
    
    [footView addSubview:addAplipayBtn];
    
    self.tableView.tableFooterView = footView;

    //开启侧滑
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
}



-(void)leftBarButtonItemClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return dataArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
        return 64;
    
}

//侧滑－－删除
//先要设Cell可编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
    
}

//修改编辑按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

//进入编辑模式，按下出现的编辑按钮后
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [dataArr removeObjectAtIndex:indexPath.section];
    
    [self.tableView reloadData];
    
}

//定义编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LSCashAlipayCell * cell = [self.tableView dequeueReusableCellWithIdentifier:lSCashAlipayCell];
    if (!cell) {
        cell = [[LSCashAlipayCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:lSCashAlipayCell];
    }
    
    NSDictionary * dict = dataArr[indexPath.section];
    
    cell.alipayID.text = [NSString stringWithFormat:@"%@(%@)",dict[@"alipayUserID"],dict[@"nameTX"]];
    
    cell.accessoryType = UITableViewCellAccessoryNone;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self showRoundProgressWithTitle:@"请稍后..."];
    
    NSDictionary * dict = dataArr[indexPath.section];
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];//取消选中状态
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"支付" message:nil delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
    alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
    
    UITextField *title = [alert textFieldAtIndex:0];
    title.borderStyle = UITextBorderStyleNone;
    title.textColor = [UIColor redColor];
    title.textAlignment = NSTextAlignmentCenter;
    title.font = [UIFont systemFontOfSize:16.0f];
    title.text = [NSString stringWithFormat:@"￥%.2f",[self.payNo floatValue]];
    title.userInteractionEnabled = NO;
    
    UITextField *passwordField = [alert textFieldAtIndex:1];
    [passwordField setSecureTextEntry:YES];
    passwordField.placeholder = @"请输入支付密码";
    
    
    [alert showAlertWithCompletionHandler:^(NSInteger buttonIndex) {
        if(buttonIndex == 1){
            UITextField *tf=[alert textFieldAtIndex:0];
            if (tf.text.length > 0) {
                
                NSMutableDictionary * dic = [NSMutableDictionary  dictionary];
                dic[@"token"] = allConstMemberToken;
                dic[@"cashUserId"] = allConstMemberid;
                dic[@"money"] = self.payNo;
                dic[@"caskWhere"] = @"1";
                dic[@"cashAccount"] = dict[@"alipayUserID"];
                dic[@"cashName"] = dict[@"nameTX"];
                dic[@"payPass"] = tf.text;
                
                [LSHttpTool sendHttpData:@"withdrawalsUser_app_pay.im.lensum" params:dic success:^(id responseObject) {
                    
                    NSDictionary *dicInfo = [NSDictionary dictionaryWithDictionary:responseObject];
                    
                    [self hideBubble];
                    // 跳转至支付成功页面
                    LSPayResultVC * LSPayResult = [[LSPayResultVC alloc]init];
                    [LSPayResult setCashTypeWithExchangeSuccess];
                    [self.navigationController pushViewController:LSPayResult animated:YES];
                    
                } failure:^(NSError *error) {
                    
                    // 跳转至支付成功页面
                    LSPayResultVC * LSPayResult = [[LSPayResultVC alloc]init];
                    [LSPayResult setCashTypeWithExchangeFailed];

                    
                }];
                
            }else{
                
                NSString *strMsg = @"密码不能为空";
                
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:strMsg delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
                [alert show];
                
            }
        }
    }];
    
}

-(void)addNewAlipayUser{

    LSAddAlipayUserID * lSAddAlipayUser = [[LSAddAlipayUserID alloc]init];
    [self.navigationController pushViewController:lSAddAlipayUser animated:YES];

}

-(void)viewWillDisappear:(BOOL)animated{

    [super viewWillDisappear:YES];
    [dataArr writeToFile:fileName atomically:YES];

}
@end
